package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.pipe.controller.dto.BowQuery;
import com.deyuanyun.pic.pipe.controller.vo.merge.BowMergeVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.CelbowVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.ElbowVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.BowService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * “弯头”控制类（包含冷弯弯头 和 热煨弯头）
 *
 * @作者:严祥
 * @创建时间: 2015年9月14日
 * @类名称: PipeElbowController
 *
 * History: [huanglw][2016-10-21][重构整个“弯头”所有后台程序]
 */
@Controller
@RequestMapping("/pipe/bow")
public class BowController extends PipeBaseController {

	@Resource
	private BowService bowService;

	/**
	 * 跳转到 弯头(包含冷热弯头) 页面
	 *
	 * @return
     */
	@RequestMapping("/toBow")
	public ModelAndView toBow() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/basedata/pipe/pipeElbow");
		return mv;
	}

	/**
	 * 根据条件分页 合并查询 弯头信息（包含冷弯弯头 和 热煨弯头）
	 *
	 * @param bowQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(BowQuery bowQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		jsonBean.setList(new BowMergeVO().fromDomainList(bowService.queryBow(bowQuery)));
		AjaxSupport.sendSuccessText("success", jsonBean);
	}

	/**
	 * 根据id查询 热煨弯头 详情
	 *
	 * @param id
	 */
	@RequestMapping("/findElbow.json")
	@ResponseBody
	public void findElbow(String id) {
		ElbowVO elbowVO = new ElbowVO();
		elbowVO.fromDomain(bowService.findElbow(id));
		AjaxSupport.sendSuccessText("success", elbowVO);
	}
	/**
	 * 根据id查询 冷弯弯头 详情
	 *
	 * @param id
	 */
	@RequestMapping("/findCelbow.json")
	@ResponseBody
	public void findCelbow(String id) {
		CelbowVO celbowVO = new CelbowVO();
		celbowVO.fromDomain(bowService.findCelbow(id));
		AjaxSupport.sendSuccessText("success", celbowVO);
	}

	/**
	 * 根据条件 导出“弯头”
	 *
	 * @param bowQuery
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public void export(BowQuery bowQuery) throws Exception{

		Page<ElbowVO> elbowList = new ElbowVO().fromDomainList(bowService.queryElbow(bowQuery));
		Page<CelbowVO> celbowList = new CelbowVO().fromDomainList(bowService.queryCelbow(bowQuery));

		// 热煨
		List<ExcelUtil.SheetParam> list = new ArrayList<ExcelUtil.SheetParam>();
		String[] columnKeys =  {"pipeLineName", "elbowNumber", "ugwwId", "dgwwId", "mileage",
				"material", "antisepticMaterial", "pressureRating", "elbowAngle","elbowType",
				"elbowCurvrad", "outletDiameter","outletWallThickness", "outletConnectionType","manufacturer",
				"coatingManufacturer", "manufacturedDate","recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(columnKeys, BeanConverter.toListMap(elbowList), 2));
		// 冷弯
		String[] lineColumnKeys = {"pipeLineName", "elbowNumber","pipeNumber", "mileage", "elbowAngle",
				"elbowCurvrad", "direction", "curveLength", "tangentLength", "arcLength",
				"coatingmAterial", "ugwwId", "dgwwId", "recorder", "recordTime", "remark"};
		list.add(new ExcelUtil.SheetParam(lineColumnKeys, BeanConverter.toListMap(celbowList), 1));

		PipeExportEnum pipeExportEnum = PipeExportEnum.Bow;
		DownExcelUtil.export(pipeExportEnum.getFileName(), pipePath+ pipeExportEnum.getTempPath(), list);
	}

}
